<?php

// $Id$

require_once($WSC->get_site_inc_dir().'_sys/site_web_page_wed.class.php');
require_once(TANGRA_MAIN_DIR.'grids/grid_ctrl.class.php');

require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


abstract class Site_Page_W_Grid extends Site_Web_Page_WED {
	protected $grid_ctrl;

	public function init() {
		parent::init();

		$this->init_views();

		$tvm = $this->get_tvm();
		$this->_create_grid_ctrl();

		$set_page_event = $this->grid_ctrl->get_grid_set_page_event();

		$this->wed->add_wed_event_action_pair($set_page_event, 'set_page_event');
	}


	protected function init_views() {
		$v = $this->create_view();
		$this->add_view($v);
	}


	protected function default_event($page_num = NULL) {
		$dbc = $this->get_dbc();
		$this->grid_ctrl->process($dbc, $page_num);

		$dbc->disconnect();

		$this->add_exports($this->grid_ctrl->get_tple_exports());

		$view = $this->get_view('default');


		return $view;
	}


	protected function set_page_event($page_num) {
		return $this->default_event($page_num);
	}


	private function _create_grid_ctrl() {
		$grid_ctrl = $this->create_grid_ctrl();
		if ($grid_ctrl instanceof Grid_Ctrl) {
			$this->grid_ctrl = $grid_ctrl;
		} else {
			throw new Tangra_Module_Exception('create_grid_ctrl() returned object that is not instance of Grid_Ctrl.');
		}

		return $grid_ctrl;
	}


	public function get_grid_ctrl() {
		return $this->grid_ctrl;
	}


	abstract protected function create_grid_ctrl();
}

